
package Datos;

import Entidades.Libros;
import Interfaces.ILibros;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
// Mathias Calache: profe yo tuve que comitiar lo de rafa porque no le anda el netbeans
/**
 *
 * @author Rafael Castelo
 */
public class DBLibros2 implements ILibros {

    private Connection connection=new MiConexion().getConection();

    @Override
    public ArrayList<Libros> conslibros() {
        ArrayList<Libros> miarray=new ArrayList<Libros>();
              try{
        CallableStatement statement;
        statement=connection.prepareCall("{call ListaTodoLibros()}");
        ResultSet resultset=statement.executeQuery();
        Libros nodo1=new Libros();
        while (resultset.next()) {
            nodo1.setnombre(resultset.getString(1));
            nodo1.setid(resultset.getInt(2));
            miarray.add(nodo1);
        }
        
        
        return miarray;
      } catch (Exception ex) {
            System.out.println("error al consultar libros");
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean IngresarLibros(Libros nodo) {
          try {
            CallableStatement statement;
            statement = connection.prepareCall("{call AgregaLibro (?)}");
            statement.setString(1, nodo.getnombre());
            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al agregar calificaciones");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean modificar(Libros nodo) {
                try {
            CallableStatement statement;
            statement = connection.prepareCall("{call ModificaLibros (?,?)}");
            statement.setString(1, nodo.getnombre());
            statement.setInt(2, nodo.getid());
            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al agregar calificaciones");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Libros consultalibros(String libro) {
                try{
        CallableStatement statement;
        statement=connection.prepareCall("{call FiltroLibros(?)}");
        statement.setString(1, libro);
        ResultSet resultset=statement.executeQuery();
        Libros nodo1=new Libros();
        nodo1.setnombre(resultset.getString(1));
        nodo1.setid(resultset.getInt(2));
        
        return nodo1;
      } catch (Exception ex) {
            //System.out.println("error al consultar");
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean eliminalibros(Libros nodo) {
                try {
            CallableStatement statement;
            statement = connection.prepareCall("{call EliminaLibros (?)}");
            statement.setInt(1,nodo.getid());
            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al eliminar calificacion");

            return false;
        }
        return true;
    }

    @Override
    public boolean validaLibros(String nlib) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public DefaultTableModel listalibros() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ArrayList<Libros> arrayfiltrolibros(String filtro) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public DefaultTableModel listalibros(String filtro) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    }
